
class Thread4 extends Thread {
    private String name;
    private Object prev;
    private Object self;

    public Thread4(String name, Object prev, Object self) {
        this.name = name;
        this.prev = prev;
        this.self = self;
    }

    @Override
    public void run() {
        int i = 5;
        while (i > 0) {
            synchronized (prev) {
                synchronized (self) {
                    System.out.println(name);
                    i--;
                    self.notify();
                }
                try {
                    prev.wait();
                } catch (InterruptedException e) {
                    System.out.println(name+"线程中断暂停");
                }
            }
        }
    }
}

public class T4 {
    public static void main(String[] args) {
        Object a = new Object();
        Object b = new Object();
        Object c = new Object();
        Thread4 t1 = new Thread4("A", a, b);
        Thread4 t2 = new Thread4("B", b, c);
        Thread4 t3 = new Thread4("C", c, a);
        t1.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t3.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
        t2.interrupt();
        t3.interrupt();
    }
}
